package com.bit.cfdi.web.domain;

import static javax.persistence.GenerationType.IDENTITY;

import java.io.Serializable;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.Version;

@Entity
@Table(name = "contacto")
public class Contacto implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6107921113967643640L;
	
	// Fields
	private Long idContacto;
	private Long ultimaModificacionPor;
	private Calendar fechaModificacion;
	private Set<Email> emails = new HashSet<Email>(0);
	private Set<Telefono> telefonos = new HashSet<Telefono>(0);
	
	private String emailString;
	private String telefonoString;

	// Property accessors
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id_contacto", nullable = false)
	public Long getIdContacto() {
		return this.idContacto;
	}

	public void setIdContacto(final Long idContacto) {
		this.idContacto = idContacto;
	}

	@Column(name = "ultima_modificacion_por")
	public Long getUltimaModificacionPor() {
		return this.ultimaModificacionPor;
	}

	public void setUltimaModificacionPor(final Long ultimaModificacionPor) {
		this.ultimaModificacionPor = ultimaModificacionPor;
	}

	@Column(name = "fecha_modificacion")
	@Temporal(TemporalType.TIMESTAMP)
	@Version
	public Calendar getFechaModificacion() {
		return this.fechaModificacion;
	}

	public void setFechaModificacion(final Calendar fechaModificacion) {
		this.fechaModificacion = fechaModificacion;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "contacto")
	public Set<Email> getEmails() {
		return this.emails;
	}

	public void setEmails(final Set<Email> emails) {
		this.emails = emails;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "contacto")
	public Set<Telefono> getTelefonos() {
		return this.telefonos;
	}

	public void setTelefonos(final Set<Telefono> telefonos) {
		this.telefonos = telefonos;
	}

	@Transient
	public String getEmailString() {
		emailString = "";
		for (Email email : emails) {
			if (email != null) {
				emailString +=  email.getEmail() + " ";
			}
		}
		return emailString;
	}

	@Transient
	public String getTelefonoString() {
		telefonoString = "";
		for (Telefono telefono : telefonos) {
			if (telefono != null) {
				telefonoString += telefono.getNumero() != null ? telefono.getNumero() + " " : "";
			}
		}
		return telefonoString;
	}

}